﻿using System;

namespace NRadioTray.App.Domain
{
    [Serializable]
    public class Radio
    {
        public Radio()
        {
            this.Id = Guid.NewGuid();
        }

        public Radio(string name)
        {
            this.ValidateParams(name);

            this.Id = Guid.NewGuid();
            this.Name = name;
            this.Url = null;
        }
        
        public Radio(string name, string url)
        {
            this.ValidateParams(name, url);

            this.Id = Guid.NewGuid();
            this.Name = name;
            this.Url = url;
        }

        public Guid Id { get; set; }

        public string Name { get; set; }

        public string Url { get; set; }

        public Guid Parent { get; set; }

        public void SetParent(Radio parent)
        {
            if (parent.Id.Equals(this.Id))
            {
                throw new ArgumentException("Parent and child is this same object.");
            }

            this.Parent = parent.Id;
        }

        public override string ToString()
        {
            return string.Format("{0} - {1}", this.Name, this.Url);
        }

        public override bool Equals(object obj)
        {
            if (!(obj is Radio))
            {
                return false;
            }

            return base.Equals(obj as Radio);
        }

        public override int GetHashCode()
        {
            return this.Id.GetHashCode();
        }

        private void ValidateParams(string name)
        {
            if (name == null || name == string.Empty)
            {
                throw new ArgumentNullException("name");
            }
        }   

        private void ValidateParams(string name, string url)
        {
            this.ValidateParams(name);

            if (url == null || url == string.Empty)
            {
                throw new ArgumentNullException("name");
            }
        }

        public Radio Copy()
        {
            return new Radio
            {
                Name = this.Name,
                Url = this.Url,
                Id = this.Id,
                Parent = this.Parent
            };
        }
    }
}
